// 隐藏未使用代码的警告
#![allow(dead_code)]

// 网页事件
#[derive(Debug)]
enum WebEvent {
    // 单元结构
    PageLoading,  // 0
    PageUnloaded, // 1
    // 元组
    KeyPress(char), // 输入字符
    Paste(String),  // 粘贴字串
    // struct
    Click { x: i64, y: i64 }, // 鼠标点击位置
}

// 枚举作为函数的参数
fn inspect(event: WebEvent) {
    match event {
        WebEvent::PageLoading => println!("page loading"),
        WebEvent::PageUnloaded => println!("page unloaded"),
        WebEvent::KeyPress(c) => println!("key press a char: {}", c),
        WebEvent::Paste(s) => println!("paste string: {}", s),
        WebEvent::Click { x, y } => println!("click at point {{x: {x}, y: {y}}}"),
    }
}

// 别名
type We = WebEvent;

// self 别名
impl WebEvent {
    fn run(&self) {
        match self {
            WebEvent::PageLoading => println!("loading page"),
            _ => println!("其他事件"),
        }
    }
}

fn main() {
    println!("枚举");

    let pressed = WebEvent::KeyPress('x');
    let pasted = WebEvent::Paste("粘贴一句话".to_string());
    let pasted1 = WebEvent::Paste("粘贴一句话".to_owned()); // 原示例中用了 to_owned
    let click = WebEvent::Click { x: 300, y: 230 };
    let load = WebEvent::PageLoading;
    let unload = WebEvent::PageUnloaded;

    inspect(pressed);
    inspect(pasted);
    inspect(pasted1);
    inspect(click);
    inspect(load);
    inspect(unload);

    println!("别名: {:?}", We::PageLoading);

    println!("self 别名: ");
    let load = WebEvent::PageLoading;
    load.run();

    // use
    use WebEvent::*;
    let load = PageLoading;
    println!(
        "用 use 直接使用 enum 值 use WebEvent::*  PageLoading:{:?}",
        load
    );
}
